// pages/search/search.js
import { request} from "../../request/index.js"
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  
  data: {
    goods:[],
    isFocus:false,//取消输入按钮
    inputValue:''//获取输入框的值
  },
  TimeId:-1,//防抖

  // 跟踪输入框
  handleInput(e){
    const {value}=e.detail;
    if(!value.trim()){//若输入框为空
      this.setData({
        goods:[],isFocus:false
      })
      return;
    }
    this.setData({
      isFocus:true
    })
    clearTimeout(this.TimeId);
    this.TimeId=setTimeout(()=>{this.qsearch(value);},1000);//设置一秒后执行括号内的函数片段——防抖
  },

  async qsearch(query){
    const goods=await request({url:'/goods/qsearch',data:{query}});
    this.setData({goods});
  },

  //清除按钮
  handleClear(){
    this.setData({
      inputValue:'',
      isFocus:false,
      goods:[]
    })
  }
})